package com.examples.jmsexamples;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * @Description 点对点
 * @Author benjamin
 * @Date 2020/1/21 11:50
 * @Version
 */
public class QueueProducer {

    public final static String USERNAME = ActiveMQConnection.DEFAULT_USER;

    public final static String PASSWORD = ActiveMQConnection.DEFAULT_PASSWORD;

    public final static String BROKER_URL = ActiveMQConnection.DEFAULT_BROKER_URL;

    /**
     * 1、 创建连接工厂
     * 2、 创建连接
     * 3、 启动连接
     * 4、 创建会话
     * 5、 创建队列
     * 6、 创建生产者
     * 7、 创建消息
     * 8、发送消息
     * 9、 提交事务
     * 10、关闭连接
     */

    public static void main(String[] args) {

        ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(USERNAME, PASSWORD, BROKER_URL);

        try {
            Connection connection = factory.createConnection();
            connection.start();
            Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
            Queue queue = session.createQueue("activemq-queue-test1");
            MessageProducer producer = session.createProducer(queue);
            TextMessage message = session.createTextMessage("Hello World!");
            producer.send(message);
            session.commit();

            session.close();
            connection.close();
        } catch (JMSException e) {
            e.printStackTrace();
        }
    }
}
